package com.jsnkj.basic.activity

import android.os.Bundle
import android.view.View
import com.jsnkj.basic.LibraryConfig
import com.jsnkj.basic.R
import com.jsnkj.basic.databinding.ActivityGoHeadBinding
import com.jsnkj.basic.dialog.TextShowDialog
import com.jsnkj.basic.mvvm.BaseMVVMActivity
import com.jsnkj.basic.util.ErrorResultUtil

//成为团长
class GoHeadActivity : BaseMVVMActivity<ActivityGoHeadBinding, GoHeadViewModel>(),
    View.OnClickListener {
    override val layoutId: Int
        get() = R.layout.activity_go_head


    var txtDialog: TextShowDialog? = null

    override fun initView(savedInstanceState: Bundle?) {
        mVDB.clickListener = this

        mVM.postHttpResult.observe(this) { entity ->
            dismissDialog()
            com.jsnkj.basic.wxapi.WechatHelper.getInstance(this@GoHeadActivity.application, LibraryConfig.WX_APP_ID)
                .PayWXOrder(this@GoHeadActivity, entity.payload, object :
                    com.jsnkj.basic.wxapi.PayResultListener {
                    override fun onPayResult(isSuccess: Boolean, message: String?) {

                        message?.let { msg ->
                            if (!isSuccess) {
                                com.jsnkj.basic.util.ToastUtils.show(msg)
                            } else {
                                if (txtDialog?.isShowing == true) {
                                    txtDialog?.dismiss()
                                }

                                txtDialog = TextShowDialog(this@GoHeadActivity, "交易信息", msg,
                                    object : TextShowDialog.onItemClickListener {
                                        override fun txtItemClick() {

                                        }
                                    })
                                txtDialog?.show()
                            }
                        }
                    }

                    override fun onPayCancel() {
                        com.jsnkj.basic.util.ToastUtils.show("取消支付")
                    }
                })
        }


        mVM.errorResult.observe(this) {
            dismissDialog()
            ErrorResultUtil.errorResultToast(this@GoHeadActivity, it)
        }
    }


    override fun onClick(view: View) {
        when (view.id) {
            R.id.image_return -> {
                onBackPressed()
            }

            R.id.txt_go_head -> {
                showDialog()
                mVM.postUserInviteCode()
            }
        }
    }


}